package challengeGUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import control.SimpleTimer;

import mover.GameMover;
import mover.SimpleMover;
import animation.Animator;
import animation.Check;
import challengesManager.ChallengeManager;
import elements.GameElement;
import elements.GameElementFactory;


public class FallingBoxGUI extends ChallengeGUI{
	private Animator animator;
	private GameElement fallingelement;
	private GameElement box;
	private Random random;
	private Check check = new Check() {
		private boolean buzz = true, fallFade = true;
		
		@Override
		public void check() {
			// Just some references to make it easy
			FallingBoxGUI gui = FallingBoxGUI.this;
			int coord = gui.fallingelement.getMovedY();
			int border = gui.box.getMovedY();

			// Save
			if(border == 0 && coord == 0) return;

			// 1/3 of the way
			if(fallFade && coord >= gui.getHeight()/3) {
				gui.fallingelement.fadeOut(true);
				fallFade = false;
			}

			// Did the element entered the box?
			if(buzz && (coord >= border)) {
				gui.manager.perfectBuzzMoment(true);
				buzz = false;
			}

			// Did the element hit the border of the gui?
			if(coord >= gui.getHeight()) {
				System.out.println("Bla");
				gui.fallingelement.fadeOut(false);
				gui.fallingelement.setTransparency(1.0f);
			
				check.reset();
				gui.manager.animationOver();
			}
		}

		@Override
		public void reset() {
			buzz = true;
			fallFade = true;
			FallingBoxGUI.this.manager.perfectBuzzMoment(false);
		}
	};

	public FallingBoxGUI(ChallengeManager manager) {
		super(manager);

		random = new Random(System.currentTimeMillis());

		animator = new Animator(this);
		animator.addCheck(check);
	}

	@Override
	public void newRound() {
		animator.stop();
		animator.clear();

		int x = myDim.width, y = myDim.height;

		// Box
		box = Animator.factory.newGameRectangle(GameElement.NO_MOVEMENT, scaler, y/4, 3*x/4);
		box.setXY(x/8, 3*y/4);
		box.setColor(Color.ORANGE);

		// Mover
		GameMover mover = new SimpleMover(0, 1);
		mover.setYMult(random.nextDouble()*4 + 2);
		// Fallingelement
		fallingelement = Animator.factory.newRandomElement(mover, scaler,
				(y/16 + random.nextInt(y/8)),
				(x/16 + random.nextInt(x/8)),
				GameElementFactory.withoutChar);
		fallingelement.setRandomColor();
		fallingelement.setMinimalFade(0);
		// Middle of the Window
		fallingelement.setXY(x/2 - fallingelement.getWidth()/2, 10);

		animator.add(fallingelement);
		animator.add(box);

		animator.start();
	}


	@Override
	public void freeze() {
		animator.stop();
	}


	@Override
	public void unfreeze() {
		animator.start();
	}

	@Override
	public void reveal(final int info) {	
		animator.stop();

		fallingelement.setXY(fallingelement.getUnscaledMovedX(), fallingelement.getUnscaledMovedY());
		fallingelement.setTransparency(1.0f);
		fallingelement.fadeOut(false);
		fallingelement.setMover(GameElement.NO_MOVEMENT);
		box.fadeOut(true);
		
		animator.clear();
		animator.add(fallingelement);
		animator.add(box);
		animator.start();
		
		SimpleTimer.pause(1000, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// Right or Wrong?
				animator.setRightOrWrong(((info==-1)?wrong:right), FallingBoxGUI.this.getWidth()/2, FallingBoxGUI.this.getHeight()/2);
			}
		});
		
		// Right or Wrong?
//		animator.setRightOrWrong(((info==-1)?wrong:right), this.getWidth()/2, this.getHeight()/2);
		
		SimpleTimer.pause(2500, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				check.reset();
				manager.animationOver();
			}
		});

//		check.reset();
	}

	@Override
	public void end() {
		animator.stop();
	}
}
